from django import forms
from django.contrib import admin
from django.contrib.auth.models import Group
from django.contrib.auth.admin import UserAdmin as BaseUserAdmin
from django.contrib.auth.forms import ReadOnlyPasswordHashField
from django.core.exceptions import ValidationError

from certificate_manager.models import User

class UserCreationForm(forms.ModelForm):

        password1 = forms.CharField(label='密码', widget=forms.PasswordInput)
        password2 = forms.CharField(label='密码确认', widget=forms.PasswordInput)

        class Meta:
                model = User
                fields = ('name', 'department', 'telephone', 'email')

        def clean_password2(self):
                # Check that the two password entries match
                password1 = self.cleaned_data.get("password1")
                password2 = self.cleaned_data.get("password2")
                if not password1 or not password2 or password1 != password2:
                    raise ValidationError("两次输入内容不匹配")

                if len(password1) < 8:
                    raise ValidationError('密码至少8位')
                return password2

        def save(self, commit=True):
                # Save the provided password in hashed format
                user = super().save(commit=False)
                user.set_password(self.cleaned_data["password1"])
                if commit:
                    user.save()
                return user

class UserChangeForm(forms.ModelForm):

        password1 = forms.CharField(label='密码', widget=forms.PasswordInput)
        password2 = forms.CharField(label='密码确认', widget=forms.PasswordInput)

        class Meta:
                model = User
                fields = ('name', 'department', 'telephone', 'email')

        def clean_password2(self):
                # Check that the two password entries match
                password1 = self.cleaned_data.get("password1")
                password2 = self.cleaned_data.get("password2")
                if not password1 or not password2 or password1 != password2:
                    raise ValidationError("两次输入内容不匹配")

                if len(password1) < 8:
                    raise ValidationError('密码至少8位')
                return password2

        def save(self, commit=True):
                # Save the provided password in hashed format
                user = super().save(commit=False)
                user.set_password(self.cleaned_data["password1"])
                if commit:
                    user.save()
                return user

class UserAdmin(BaseUserAdmin):
        form = UserChangeForm
        add_form = UserCreationForm

        list_display = ('uid', 'name', 'department')
        list_filter = ('department',)
        fieldsets = (
                (None, {'fields': ('name', 'department', 'telephone', 'email', 'password1', 'password2')}),
        )

        add_fieldsets = (
                (None, {
                    'classes': ('wide',),
                    'fields': ('uid', 'name', 'department', 'email', 'telephone', 'password1', 'password2'),
                }),
        )
        search_fields = ('name', 'uid')
        ordering = ('uid',)
        filter_horizontal = ()

admin.site.register(User, UserAdmin)
admin.site.unregister(Group)
